/*
 * @文件名称： ALog.java 
 * @文件描述：
 * @版权所有: (C)2008-2012 
 * @创建日期: 2012-7-18  
 * @作者：           lizhen_a  
 */
package com.yidin.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; 
import java.io.OutputStreamWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;

 
import android.os.Environment;
import android.util.Log;

 


public class ALog {
	
	//****************************************************************//
	// <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	//****************************************************************//
	
	public static final String logFile = "Marketing.txt";
	public static final String  logDir = "logger";
	public static final boolean isLog = true;
	
	//****************************************************************//
 
	
	private static final int KHexMax = 48;
    private static final int KStrMax = 16;
    private static final String KSpaceStr = String.format("%10s", "");
    private static final String KFilledStr = String.format("%48s", "");
    
	private static final String KLogFormat = "%s  %s  %s";
	 
	private static final String KPathFormat ="%s%s%s%s";

	private static final String KFilePath = String.format(KPathFormat, 
													Environment.getExternalStorageDirectory().getPath(), 
													File.separator,
													logDir,
													File.separator);
	
	private static final File mFile = new File(KFilePath,logFile);
	
	public static void __LogFile_Del__()
	{
		if (mFile.exists())
		{
			mFile.delete();
		}

	}

	
	public static void __Log__(String content)
	{
		if (isLog)
		{ 
			Log.i(getInfo(), content);
			writeFile(String.format(KLogFormat, getTime(),getInfo(),content));
		}

	}
	
	public static void __HEX__(String content)
	{
		if (isLog)
		{
			String content2 = String.format("%s ",content);
			int content2Len = content2.length();
			
			StringBuffer tmpStr = new StringBuffer(KStrMax);  
			StringBuffer tmpHex = new StringBuffer(KHexMax);
			int strLenSum = 0;
			
			for(int i = 0; i < content2Len; ++i)
			{
				String str = content2.substring(i, i + 1);
				int dec = (int)str.charAt(0);
				
				if (dec < 32)
				{
					continue;
				}
				String hex = String.format("%02x ", dec);
				
 
				tmpStr.append(str);
				tmpHex.append(hex);
				
				strLenSum += hex.length()/2;
				
				if ((strLenSum > KStrMax) || (tmpHex.length() > KHexMax) || (content2Len - 1 == i))
				{
					int strLen = tmpStr.length();
					tmpStr.delete(strLen - str.length(), strLen);
					
					int hexLen = tmpHex.length();
					tmpHex.delete(hexLen - hex.length(),hexLen);
					
					int needLen = KHexMax - tmpHex.length();
					String spaStr = String.format("%s%s", KSpaceStr,KFilledStr.substring(0,needLen));
		 
					String conStr = String.format("%s%s%s",tmpHex,spaStr,tmpStr);
					String lineStr = String.format(KLogFormat, getTime(),getInfo(),conStr);
					
					ALog.writeFile(lineStr);
					Log.i(getInfo(),conStr);
					
					i = (content2Len - 1 == i) ? i : --i;
					
					tmpStr = new StringBuffer(KStrMax);
					tmpHex = new StringBuffer(KHexMax);
					strLenSum = 0;
				}
				

			}
 
			
		}
	}
	
	private static void writeFile(String str)
	{  
 
		if (!mFile.exists()) 
		{
			try {
				File path = new File(KFilePath);
				path.mkdir();
				
				mFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 

		try {
//			FileWriter fw = new FileWriter(mFile, true); 
//			fw.write(String.format("%s%s",str,"\r\n"));
//			fw.close(); 
			
			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(mFile, true),"UTF-8");
			out.write(String.format("%s%s",str,"\r\n"));
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
	
	
	private static String getTime() 
	{	
		Date now = new Date(System.currentTimeMillis()); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
		return sdf.format(now); 
	} 
	
	private static String getInfo()
	{
		StackTraceElement traceElement = Thread.currentThread().getStackTrace()[4]; 
		return traceElement.toString();
	}

}
 